﻿package org.ilanchong.droidcontroller;

import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.List;

import org.ilanchong.droidcontroller.socket.DroidClient;
import org.ilanchong.droidcontroller.socket.MyDatagramSocket;
import org.ilanchong.droidcontroller.ui.CommandResultInfo;
import org.ilanchong.droidcontroller.ui.ComputerResultInfo;

public class DroidController {

	public static CommandResultInfo sendCommond(List<ComputerInfo> cpList, int port, String commond) {
		DroidClient client = null;

		CommandResultInfo resInfo = new CommandResultInfo(true, "");
		StringBuilder message = new StringBuilder();

		if (cpList == null || (cpList.size() == 0)) {
			resInfo.setResult(false);
			resInfo.setMessage("没有选择控制电脑，请先获取控制电脑。");
			return resInfo;
		}

		for (int i = 0; i < cpList.size(); i++) {
			CommandResultInfo tmpResInfo = new CommandResultInfo(true, "");
			ComputerInfo c = (ComputerInfo) cpList.get(i);

			try {
				client = new DroidClient(c.getIP(), port);
				client.setTimeOut(Utils.CONNECT_TIMEOUT);
				client.sendCommand(commond);

			} catch (UnknownHostException e) {

			} catch (IOException e) {

			} finally {
				try {
					client.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			if (tmpResInfo.getResult() == true) {
				tmpResInfo.setResult(true);
				tmpResInfo.setMessage("命令发送到控制电脑 " + c.getIP() + "-"
						+ c.getHostName() + " 成功;");
			}
			message.append(tmpResInfo.getMessage());
		}

		resInfo.setMessage("给所选控制电脑发送命令完成。执行情况：" + message.toString());

		return resInfo;
	}
	
	public static ComputerResultInfo getIPHostname(String broadcast_ip, String localIP, int broadcast_port, String command) {
		ComputerResultInfo resInfo = new ComputerResultInfo(false, "", new ComputerInfo());
		
		MyDatagramSocket myDatagramSocket = null;
		try {
			myDatagramSocket = new MyDatagramSocket();
			myDatagramSocket.setSoTimeout(Utils.CONNECT_TIMEOUT);
			// command = "cmd:android->pc:getiphostname";
			myDatagramSocket.sendMessage(InetAddress.getByName(broadcast_ip), broadcast_port, command);

			//ip and hostname
			String str = myDatagramSocket.receiveMessage();

			String ips_hostname = "";
			if (str != null && !"".equals(str.trim())) {
				ips_hostname = str.trim();
				int _i = ips_hostname.lastIndexOf("#");

				
				resInfo.getComputer().setHostName(ips_hostname.substring(_i + 1));
				resInfo.getComputer().setIP(Utils.getPriorityIP(ips_hostname.substring(0, _i), localIP));
				resInfo.setResult(true);
				resInfo.setMessage("");
			} else {
				resInfo.setResult(false);
				resInfo.setMessage("无法 接收到IP, 接收到IP 为空");
			}

		} catch (SocketException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (myDatagramSocket != null && !myDatagramSocket.isClosed())
				myDatagramSocket.close();
		}
		return null;
	}
	
}
